﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;

namespace TestAgentDatabases
{
    class SQLServerCompactTest : DatabaseTest
    {
        protected override void InsertOrder(int databaseId, int orderIndex, IDbConnection connection, ref IDbCommand dbCommand)
        {
            if (dbCommand == null)
            {
                dbCommand = connection.CreateCommand();
                dbCommand.CommandText = "insert into " + TableName + " (amount, marketId, executionDate, status) values (@amount, @marketId, @executionDate, @status)";
                AddParameter(dbCommand, "@amount", DbType.Currency);
                AddParameter(dbCommand, "@marketId", DbType.Int32);
                AddParameter(dbCommand, "@executionDate", DbType.DateTime);
                AddParameter(dbCommand, "@status", DbType.Int32);
            }

            ((IDbDataParameter)dbCommand.Parameters["@amount"]).Value = orderIndex;
            ((IDbDataParameter)dbCommand.Parameters["@marketId"]).Value = 1;
            ((IDbDataParameter)dbCommand.Parameters["@executionDate"]).Value = DateTime.UtcNow;
            ((IDbDataParameter)dbCommand.Parameters["@status"]).Value = 1;
            dbCommand.ExecuteNonQuery();
        }

        protected override string GetCreateTableSQL(int databaseId)
        {
            return "create table " + TableName + " (orderId integer identity primary key, marketId int not null, amount money not null, executionDate datetime not null, Status integer)";           
        }

        protected override IDbConnection CreateDatabaseConnection(int databaseId)
        {
            var connectionString = GetConnectionString(databaseId);
            var engine = new SqlCeEngine(connectionString);
            engine.CreateDatabase();
            return new SqlCeConnection(connectionString);
        }

        protected override IDbConnection GetDatabaseConnection(int databaseId)
        {
            return new SqlCeConnection(GetConnectionString(databaseId));
        }

        private string GetConnectionString(int databaseId)
        {
            return "Data Source=" + Path.Combine(DatabaseDirectory, "db" + databaseId + ".sdf");
        }
    }
}